From a45aa4ad27884d2a113907623c71d90f3111296d Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Sun, 4 Jan 2015 18:11:55 -0500 Subject: [PATCH] [cargo new] Add a new --vcs enumeration flag Replace the --git, --hg, and --no-git flags with a --vcs enumeration flag which supports the values "git", "hg", and "none". Related to #1116 --- src/bin/new.rs | 38 ++++++++++++++++++++++++++------------ src/doc/guide.md | 2 +- tests/test_cargo_new.rs | 6 +++--- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/bin/new.rs b/src/bin/new.rs index 80f6c5999..2fb43478c 100644 --- a/src/bin/new.rs +++ b/src/bin/new.rs @@ -1,18 +1,34 @@ use std::os; +use rustc_serialize::{Decodable, Decoder}; use cargo::ops; use cargo::core::MultiShell; use cargo::util::{CliResult, CliError}; +#[deriving(Show, PartialEq)] +enum VersionControl { Git, Hg, NoVcs } + +impl> Decodable for VersionControl { + fn decode(d: &mut D) -> Result { + Ok(match try!(d.read_str()).as_slice() { + "git" => VersionControl::Git, + "hg" => VersionControl::Hg, + "none" => VersionControl::NoVcs, + n => { + let err = format!("could not decode '{}' as version control", n); + return Err(d.error(err.as_slice())); + } + }) + } +} + #[deriving(RustcDecodable)] struct Options { flag_verbose: bool, flag_bin: bool, flag_travis: bool, - flag_hg: bool, - flag_git: bool, - flag_no_git: bool, arg_path: String, + flag_vcs: Option, } pub const USAGE: &'static str = " @@ -24,10 +40,9 @@ Usage: Options: -h, --help Print this message - --no-git Don't initialize a new git repository - --git Initialize a new git repository, overriding a - global `git = false` configuration - --hg Initialize a new hg repository + --vcs Initialize a new repository for the given version + control system (git or hg) or do not initialize any version + control at all (none) overriding a global configuration. --travis Create a .travis.yml file --bin Use a binary instead of a library template -v, --verbose Use verbose output @@ -37,13 +52,12 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult debug!("executing; cmd=cargo-new; args={}", os::args()); shell.set_verbose(options.flag_verbose); - let Options { flag_no_git, flag_travis, - flag_bin,arg_path, flag_git, flag_hg, .. } = options; + let Options { flag_travis, flag_bin, arg_path, flag_vcs, .. } = options; let opts = ops::NewOptions { - no_git: flag_no_git, - git: flag_git, - hg: flag_hg, + no_git: flag_vcs == Some(VersionControl::NoVcs), + git: flag_vcs == Some(VersionControl::Git), + hg: flag_vcs == Some(VersionControl::Hg), travis: flag_travis, path: arg_path.as_slice(), bin: flag_bin, diff --git a/src/doc/guide.md b/src/doc/guide.md index a8788af03..43994734d 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -33,7 +33,7 @@ $ cargo new hello_world --bin We're passing `--bin` because we're making a binary program: if we were making a library, we'd leave it off. If you'd like to not initialize a new -git repository as well (the default), you can also pass `--no-git`. +git repository as well (the default), you can also pass `--vcs none`. Let's check out what Cargo has generated for us: diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index 9906b8342..49b995817 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -24,7 +24,7 @@ fn cargo_process(s: &str) -> ProcessBuilder { test!(simple_lib { os::setenv("USER", "foo"); - assert_that(cargo_process("new").arg("foo").arg("--no-git"), + assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("none"), execs().with_status(0)); assert_that(&paths::root().join("foo"), existing_dir()); @@ -177,12 +177,12 @@ test!(git_prefers_command_line { fs::mkdir(&root.join(".cargo"), USER_RWX).unwrap(); File::create(&root.join(".cargo/config")).write_str(r#" [cargo-new] - git = false + vcs = "none" name = "foo" email = "bar" "#).unwrap(); - assert_that(cargo_process("new").arg("foo").arg("--git").cwd(td.path().clone()) + assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("git").cwd(td.path().clone()) .env("USER", Some("foo")), execs().with_status(0)); assert!(td.path().join("foo/.gitignore").exists()); -- 2.30.2